<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SwtTradeOrder;
use App\Models\SwtTradeAccountLog;
use App\Models\SwtTradeAddress;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SwtTradeOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(\App\Models\SwtTradeOrder::with(['address','user','coin','network']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('address.address','交易地址') -> copyable();
            $grid->column('user.name','用户昵称');
            $grid->column('coin.base_ccy','币种');
            $grid->column('network.name','网络');
            $grid->column('to_address','地址')->copyable();
            $grid->column('type','操作') -> using(['充币','提币']) -> label(['info','warning']);
            $grid->column('num','数量');
            $grid->column('status','状态') -> using(['待处理','已处理','已取消']) -> label(['info','success','warning']);
            $grid->column('created_at');

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('type', '操作', ['充币','提币']);
                $selector->select('status', '状态', ['待处理','已处理','已取消']);
            });
            $grid->disableCreateButton(); //关闭新增按钮
            $grid->disableEditButton();  //关闭编辑按钮
            $grid->disableDeleteButton(); //关闭删除按钮
            $grid -> actions( function (Grid\Displayers\Actions $actions) {
                if($actions -> row -> type == 1 && $actions -> row -> status == 0) {
                    $actions->quickEdit();
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtTradeOrder(), function (Show $show) {
            $show->field('id');
            $show->field('address_id');
            $show->field('user_id');
            $show->field('coin_id');
            $show->field('network_id');
            $show->field('to_address');
            $show->field('type');
            $show->field('num');
            $show->field('status');
            $show->field('desc');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtTradeOrder(), function (Form $form) {
            $form->display('id');
            $form->radio('status','状态') -> options([ '1' => '已处理', '2'=>'取消'])-> required();
            $form->textarea('desc','备注') -> required();

            $form -> saved(function (Form $form, $result) {
                if(!$result) {
                    return;
                }

                if ($form->isEditing()) {
                    $status = $form -> input('status');

                    if($status == 2) {
                        $address_id = $form -> model() -> address_id;

                        $num = $form -> model() -> num;

                        SwtTradeAddress::where('id', $address_id) -> increment('total', $num);

                        SwtTradeAccountLog::create([
                            "address_id"    => $address_id,
                            "user_id"       => $form->model()->user_id,
                            "source_type"   => 2,
                            "source_id"     => $form->model()->id,
                            "text"          => "取消充币返还" . $num,
                            "change_type"   => 0,
                            'change_amount' => $num
                        ]);
                    }
                }
            });
        });
    }
}
